import { defHttp } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { BaseResponse, PagerResponse } from '@/api/base';
import { ContentTypeEnum } from '@/enums/httpEnum';
import DemoDO from '@/api/model/DemoDO';
import DemoQuery from '@/api/model/DemoQuery';
import DemoRequest from '@/api/model/DemoRequest';
import DemoTreeDO from '@/api/model/DemoTreeDO';

// 定义接口地址
enum Api {
  /** form协议: 基本类型单参数 */
  FORM_BASIC = '/toolkit/demo/formBasic',
  /** form协议: 基本类型多参数 */
  FORM_MULTI_BASIC = '/toolkit/demo/formMultiBasic',
  /** form协议: 对象类型单参数 */
  FORM_OBJECT = '/toolkit/demo/formObject',
  /** json协议: body为基本类型数组 */
  JSON_BASIC_LIST = '/toolkit/demo/jsonBasicList',
  /** json协议: 只有body参数 */
  JSON_BODY = '/toolkit/demo/jsonBody',
  /** json协议: 路径参数 + body参数  */
  JSON_BODY_AND_PATH = '/toolkit/demo/jsonBodyAndPath/',
  /** json协议: 无参 */
  JSON_NO_BODY = '/toolkit/demo/jsonNoBody',
  /** json协议: body为对象类型数组 */
  JSON_OBJECT_LIST = '/toolkit/demo/jsonObjectList',
  /** 分页查询 */
  JSON_PAGER = '/toolkit/demo/jsonPager',
  /** json协议: 只有路径参数，无body参数  */
  JSON_PATH = '/toolkit/demo/jsonPath/',
}

// 定义接口方法
// form协议: 基本类型单参数
export function formBasic(params: { name: string }, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<DemoDO>>(
    {
      url: Api.FORM_BASIC,
      params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
    },
    {
      errorMessageMode: mode,
    },
  );
}
// form协议: 基本类型多参数
export function formMultiBasic(params: { id: number; name: string }, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<DemoDO>>(
    {
      url: Api.FORM_MULTI_BASIC,
      params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
    },
    {
      errorMessageMode: mode,
    },
  );
}
// form协议: 对象类型单参数
export function formObject(params: DemoQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<DemoTreeDO>>>(
    {
      url: Api.FORM_OBJECT,
      params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: body为基本类型数组
export function jsonBasicList(params: number[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<string>>(
    {
      url: Api.JSON_BASIC_LIST,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: 只有body参数
export function jsonBody(params: DemoRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.JSON_BODY,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: 路径参数 + body参数
export function jsonBodyAndPath(id: number, params: DemoRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.put<BaseResponse<string>>(
    {
      url: Api.JSON_BODY_AND_PATH + '/' + id,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: 无参
export function jsonNoBody(mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<DemoDO>>>(
    {
      url: Api.JSON_NO_BODY,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: body为对象类型数组
export function jsonObjectList(params: DemoRequest[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<number>>>(
    {
      url: Api.JSON_OBJECT_LIST,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 分页查询
export function jsonPager(params: DemoQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<PagerResponse<DemoDO>>>(
    {
      url: Api.JSON_PAGER,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// json协议: 只有路径参数，无body参数
export function jsonPath(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.delete<BaseResponse<boolean>>(
    {
      url: Api.JSON_PATH + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}

// 聚合接口
export default {
  formBasic,
  formMultiBasic,
  formObject,
  jsonBasicList,
  jsonBody,
  jsonBodyAndPath,
  jsonNoBody,
  jsonObjectList,
  jsonPager,
  jsonPath,
};
